package com.softeem.yuedong_server.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softeem.yuedong_server.dto.Result;
import com.softeem.yuedong_server.entity.Album;
import com.softeem.yuedong_server.entity.Music;
import com.softeem.yuedong_server.entity.Singer;
import com.softeem.yuedong_server.service.AlbumService;
import com.softeem.yuedong_server.service.MusicService;
import com.softeem.yuedong_server.service.SingerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Tag(name = "专辑管理")
@RequestMapping("/album")
@RestController
public class AlbumController extends BaseController{

    @Autowired
    private AlbumService albumService;
    @Resource
    private SingerService singerService;
    @Resource
    private MusicService musicService;

    /*
    * 专辑添加的接口，提供专辑对象，返回Result结果*/
    @Operation(summary = "专辑添加",description = "添加专辑信息，需要提供基本的参数，例如名称和发行公司")
    @PostMapping("/")
    public Result addAlbum(@RequestBody Album album){
        return albumService.save(album) ? Result.ok().setData(album).setMsg("添加成功") : Result.fail().setMsg("添加失败");
    }
    /*
    * 专辑删除的接口，提供id，返回Result结果
    * */
    @Operation(summary = "删除专辑",description = "根据提供的专辑id删除专辑，id必须提供")
    @DeleteMapping("/{id}")
    public Result deleteAlbum(@PathVariable Integer id){
        return albumService.removeById(id) ? Result.ok().setMsg("删除成功") : Result.fail().setMsg("删除失败");
    }
    /*
    * 专辑修改的接口，提供专辑对象，返回Result结果*/
    @Operation(summary = "专辑更新",description = "修改专辑信息，id必须提供")
    @PutMapping("/")
    public Result updateAlbum(@RequestBody Album album){
        return albumService.updateById(album) ? Result.ok().setData(album).setMsg("修改成功") : Result.fail().setMsg("修改失败");
    }
    /*
    * 专辑查询的接口，提供id，返回Result结果*/
    @Operation(summary = "专辑详情",description = "必须提供id，查看专辑详情，并显示专辑总所有的歌曲信息以及歌手信息")
    @GetMapping("/{id}")
    public Result findAlbum(@PathVariable Integer id){
        Album album = albumService.getById(id);
        if(Objects.nonNull(album)){
            Singer singer = singerService.getById(album.getSid());
            album.setSinger(singer);
            //根据专辑id查询所有歌曲
            List<Music> musics =  musicService.list(Wrappers.lambdaQuery(Music.class).eq(Music::getAid,id));
            album.setSinger(singer);
            album.setMusics(musics);

        }
        return album != null ? Result.ok().setData(album).setMsg("查询成功") : Result.fail().setMsg("查询失败");
    }
    /*
    * 查询专辑所有结果的接口返回Result结果*/
    @Operation(summary = "专辑列表",description = "查询所有的专辑信息，提供页码和每页大小，分页显示数据")
    @Parameters({
            @Parameter(name = "page",description = "页码",required = true),
            @Parameter(name = "limit",description = "每页大小",required = true)
    })
    @GetMapping("/list")
    public Result findAllAlbum(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer limit){
        //调用albumService的list方法
        List<Album> list = albumService.list(new Page<>(page,limit));
        //查询专辑总条数
        long count = albumService.count();
        //返回结果
        return Result.ok().setData(list).setCount(count);


    }
}
